// @flow
import React, { Component } from 'react'
import styles from './ProblemActions.css'
import { Button, Popconfirm, message, Icon, Modal } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'

type Props = {
  refreshCustomers: Function,
  data: Object,
  history: Object,
  current: number,
  pageSize: number
}
type State = {
  visible: boolean
}
class ProblemActions extends Component {
  props: Props
  state: State
  deleteCustomer: Function
  handleCancel: Function
  handleOk: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      visible: false
    }
    this.deleteCustomer = this.deleteCustomer.bind(this)
    this.handleCancel = this.handleCancel.bind(this)
    this.handleOk = this.handleOk.bind(this)
  }

  handleOk () {
    this.setState({
      visible: true
    })
  }

  handleCancel () {
    this.setState({
      visible: false
    })
  }

  deleteCustomer () {
    const { data, current, pageSize } = this.props
    universalFetch(`${__SERVICE_API__}device/customers/${data.id}`, {
      method: 'DELETE'
    })
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      message.success('客户删除成功')
      this.props.refreshCustomers(current, pageSize)
    })
     .catch((error) => {
       handleFetchError(error)
       console.error(error)
       message.error('客户删除失败')
     })
  }
  render () {
    const { history, data } = this.props
    const fileList = data.fileList
    let showFile = false // 文件是否显示
    if (fileList.length > 0) {
      showFile = true
    }
    // 编辑
    let edit = <Button
      size='small'
      type='primary'
      style={{ marginRight: '10px' }}
      onClick={() => {
        history.push({
          pathname: '/business/customer-edit',
          state: {
            data
          } })
      }}
      >
      编辑
    </Button>
    if (data.editPermission === 'no') {
      edit = ''
    }
    // 删除
    let delet = <Popconfirm title='确定要删除此客户吗'
      arrowPointAtCenter
      placement='left'
      onConfirm={this.deleteCustomer}
      >
      <Button
        size='small'
        type='danger'
        >
        删除
      </Button>
    </Popconfirm>
    if (data.delPermission === 'no') {
      delet = ''
    }
    return (
      <div className={styles['actions']}>
        {edit}
        {delet}
        {
          showFile
            ? <a title='下载附件' onClick={this.handleOk} style={{ marginLeft: 10 }}>
              <Icon type='paper-clip' style={{ fontSize: 16 }} />
            </a>
            : ''
        }
        <Modal
          title='下载附件'
          visible={this.state.visible}
          onCancel={this.handleCancel}
          footer={null}
        >
          {
            fileList.map((d, i) => {
              return <a
                style={{ display: 'block', padding: 3 }}
                key={i}
                href={d.fileUrl}
                target='_blank' rel='noopener noreferrer'
                title={d.fileName}>
                {d.fileName}
              </a>
            })
          }
        </Modal>
      </div>
    )
  }
}

export default ProblemActions
